package com.blog.admin.controller;

import com.blog.admin.Exception.MyException;
import com.blog.admin.mapper.apiMapper.ArticleMapper;
import com.blog.admin.service.BlogArticleService;
import com.blog.admin.vo.*;
import com.blog.util.Result;
import com.blog.vo.PageVO;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tangqj
 * @Description
 * @create 2020/2/18 15:14
 */
@RestController
@RequestMapping("blogArticleController")
public class BlogArticleController {
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    BlogArticleService blogArticleService;


    /**
     * 分页获取文章
     * @param page
     * @param blogArticleVO
     * @return
     */
    @RequestMapping("getBlogArticle")
    public Map<String,Object> getBlogArticle(PageVO page, BlogArticleVO blogArticleVO){
        PageInfo<BlogArticleVO> result = blogArticleService.getBlogArticle(page,blogArticleVO);
        Map<String,Object> data = new HashMap<>();
        data.put("count", result.getTotal());
        return Result.responeAjaxMap(true,"", result.getList(),data);
    }



    /**
     * 获取分类列表
     * @return
     */
    @RequestMapping("getAllBlogClassify")
    public Map<String,Object> getAllBlogClassify(){
        List<BlogClassifyVO> result = blogArticleService.getAllBlogClassify();
        return Result.responeAjaxMap(true,"", result,null);
    }


    /**
     * 文章插入
     * @param blogArticleVO
     * @return
     */
    @RequestMapping("insertBlogArticle")
    public Map<String,Object> insertBlogArticle(BlogArticleVO blogArticleVO){
        SysUserVO loginName =  (SysUserVO) SecurityUtils.getSubject().getPrincipal();
        blogArticleVO.setPkUser(loginName.getId());
        boolean result = blogArticleService.insertBlogArticle(blogArticleVO);
        return Result.responeAjaxMap(result,  result?"操作成功":"操作失败", "",null);
    }

    /**
     * 文章修改
     * @param blogArticleVO
     * @return
     */
    @RequestMapping("updateBlogArticle")
    public Map<String,Object> updateBlogArticle(BlogArticleVO blogArticleVO){
        boolean result = blogArticleService.updateBlogArticle(blogArticleVO);
        return Result.responeAjaxMap(result,  result?"操作成功":"操作失败", "",null);
    }

    /**
     * 文章审核
     * @param blogArticleVO
     * @return
     */
    @RequestMapping("updateBlogArticleStatus")
    public Map<String,Object> updateBlogArticleStatus(BlogArticleVO blogArticleVO){
        boolean result = blogArticleService.updateBlogArticleStatus(blogArticleVO);
        return Result.responeAjaxMap(result,  result?"操作成功":"操作失败", "",null);
    }

    /**
     *  修改置顶
     * @param blogArticleVO
     * @return
     */
    @RequestMapping("updateBlogArticleTop")
    public Map<String,Object> updateBlogArticleTop(BlogArticleVO blogArticleVO){
        boolean result = blogArticleService.updateBlogArticleTop(blogArticleVO);
        return Result.responeAjaxMap(result,  result?"操作成功":"操作失败", "",null);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @RequestMapping("deleteBlogArticleById")
    public Map<String,Object> deleteBlogArticleById(String id){
        boolean result = blogArticleService.deleteBlogArticleById(id);
        return Result.responeAjaxMap(result,  result?"操作成功":"操作失败", "",null);
    }

    /**
     * 分页获取文章评论
     * @param page
     * @param blogCommentVO
     * @return
     */
    @RequestMapping("getBlogComment")
    public Map<String,Object> gtBlogComment(PageVO page, BlogCommentVO blogCommentVO){
        PageInfo<BlogCommentVO> result = blogArticleService.getBlogCommentAll(page,blogCommentVO);
        Map<String,Object> data = new HashMap<>();
        data.put("count", result.getTotal());
        return Result.responeAjaxMap(true,"", result.getList(),data);
    }

    /**
     * 删除文章评论
     * @param id
     * @return
     */
    @RequestMapping("deleteBlogCommentById")
    public Map<String,Object> deleteBlogCommentById(String id){
        boolean result = blogArticleService.deleteBlogCommentById(id);
        return Result.responeAjaxMap(result,  result?"操作成功":"操作失败", "",null);
    }
}
